package com.ayoka.parenting.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import java.util.List;

/**
 * Created by jinchunping on 2018/4/25.
 */
@SuppressLint("NewApi")
public class ReportCircleProgressView extends View {

    private Paint circlePaint;//绘制矩形的画笔
    private Paint progressPaint;//绘制圆弧的画笔

    private int size;

    private List<CircleReportBean> CircleReportBeanList;

    private int[] colors = new int[]{Color.parseColor("#3eb3fe"), Color.parseColor("#ffbb62"), Color.parseColor("#181837")};
    private int[] strokeWidths = new int[]{15, 25, 15};
    private int[] radius = new int[]{50, 85, 130};

    public ReportCircleProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context,attrs);
    }

    private void init(Context context, AttributeSet attrs){
        circlePaint = new Paint();
        circlePaint.setColor(Color.parseColor("#eeeeee"));
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setAntiAlias(true);

        progressPaint = new Paint();
        progressPaint.setStrokeCap(Paint.Cap.ROUND);
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setAntiAlias(true);
    }


    @SuppressWarnings("unused")
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        int width = getMeasuredWidth();
        int height = getMeasuredHeight();
        size = Math.min(width, height);
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(size, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(size / 2, size / 2);
        if (CircleReportBeanList == null || CircleReportBeanList.isEmpty()) return;
        for (CircleReportBean circleReportBean : CircleReportBeanList) {
            circlePaint.setStrokeWidth(circleReportBean.strokeWidths);

            progressPaint.setColor(circleReportBean.colors);
            progressPaint.setStrokeWidth(circleReportBean.strokeWidths);
            drawCircle(canvas, circleReportBean.radius);
            drawProgress(canvas, circleReportBean.radius, circleReportBean.sweepAngle);
        }
    }

    private void drawCircle(Canvas canvas, int radius) {
        // 绘制圆环
        canvas.drawCircle(0, 0, radius, circlePaint);
    }

    private void drawProgress(Canvas canvas, int radius, int sweepAngle) {
        // 绘制圆环
        RectF oval = new RectF(-radius, -radius, radius, radius);
        canvas.drawArc(oval, -90, sweepAngle, false, progressPaint); //
    }


    public void setReportBeanData(List<CircleReportBean> CircleReportBeanList){
        this.CircleReportBeanList = CircleReportBeanList;
        postInvalidate();
    }

    public static class CircleReportBean{
        public int colors;
        public int strokeWidths;
        public int radius;
        public int sweepAngle;

        public CircleReportBean(int colors, int strokeWidths, int radius, int sweepAngle) {
            this.colors = colors;
            this.strokeWidths = strokeWidths;
            this.radius = radius;
            this.sweepAngle = sweepAngle;
        }
    }
}
